/*
 * ESPeachyPinata.java
 *
 * Created on January 4, 2008, 5:27 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.sprite;

import hextd.util.Cache;
import hextd.util.Geometry;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;

/**
 *
 * @author hylke
 */
public class ESPeachyPinata extends EnemySpriteBase {

	private int prevPaintTime;
	private Color bodyColor = new Color(255, 218, 185);
	private Color bodyShellColor = Color.WHITE;
	private Stroke bodyStroke;
	private Stroke bodyShellStroke;
	private Shape[] bodyShapes;
	private Shape bodyShellShape;
	private int bodyStep = 0;
	private static float reductionFactor = 0.005f;
	private static String infoText =
			"<h1>Peachy Piñata</h1>" +
			"<p>" +
			"There's a bonus inside, but this piñata is not made out of paper!" +
			"</p>";

	/**
	 * Creates a new instance of ESPeachyPinata
	 */
	public ESPeachyPinata() {
		super("Peachy Piñata");
		this.damageColor = EnemySprite.COLOR.BLUE;
		this.damageColorAnti = EnemySprite.COLOR.YELLOW;
	}

	public String getInfoString() {
		return this.infoText +
				"<p><b>Damage Reduction:</b> " + this.form.format(Math.round(reductionFactor * this.healthMax)) + "<br>" +
				"" + super.getInfoString() + "</p>";
	}

	protected void doDamage(int damage) {
		long realDamage = Math.round(damage - reductionFactor * this.healthMax);
		realDamage = Math.max(realDamage, 1) * 3 / 4;

		if (this.validTarget()) {
			this.health -= realDamage;
		}
		if (this.health <= 0) {
			this.validTarget = false;
			this.dead = true;
			this.context.addBonus();
			this.context.addScore(this.price);
			this.context.doReceive(this.price);
			this.context.removeEnemy();
		}
		this.fireStatusChangedEvent();
	}

	public void resetScale() {
		super.resetScale();
		Cache cache = this.context.getCache();


		if (cache.hasShapeArr("ESPePiShapes" + this.size.letter)) {
			this.bodyShapes = cache.getShapeArr("ESPePiShapes" + this.size.letter);
			this.bodyShellShape = cache.getShape("ESPePiShape" + this.size.letter);
		} else {
			float scale = this.context.paintScale * this.size.scaleMod;
			this.bodyShapes = new Shape[30];

			float r0 = scale / 6;
			float dr1 = r0 / 2;
			float dr2 = dr1 / this.bodyShapes.length;
			int max = this.bodyShapes.length;
			for (int i = 0; i < max; i++) {
				Area a = new Area(Geometry.createCircle(0, 0, r0 + max * dr2));
				a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 0 + i * dr2)));
				a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 1 + i * dr2)));
				a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 2 + i * dr2)));
				this.bodyShapes[i] = a;
			}
			this.bodyShellShape = new Area(Geometry.createCircle(0, 0, r0 + max * dr2));
			cache.putShapeArr("ESPePiShapes" + this.size.letter, this.bodyShapes);
			cache.putShape("ESPePiShape" + this.size.letter, this.bodyShellShape);
		}
		this.bodyStroke = new BasicStroke(1f * this.context.strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		this.bodyShellStroke = new BasicStroke(2f * this.context.strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	}

	public void paint(Graphics2D g2, int gameTime) {
		if (this.inactive || this.dead) {
			this.prevPaintTime = gameTime;
		} else {
			Stroke defaultStroke = g2.getStroke();
			this.bodyStep += gameTime - this.prevPaintTime;
			if (this.bodyStep >= this.bodyShapes.length) {
				this.bodyStep = 0;
			}
			this.prevPaintTime = gameTime;

			AffineTransform saveXform = g2.getTransform();
			g2.transform(atTranslate);

			if (this.shielded) {
				this.paintShield_bottom(g2);
			}
			this.paintHealthLine(g2);

			g2.setColor(this.bodyColor);
			g2.setStroke(this.bodyStroke);
			g2.draw(this.bodyShapes[this.bodyStep]);
			g2.setColor(this.bodyShellColor);
			g2.setStroke(this.bodyShellStroke);
			g2.draw(this.bodyShellShape);
			g2.setTransform(saveXform);

			if (this.shielded) {
				g2.transform(atTranslate);
				this.paintShield_top(g2);
				g2.setTransform(saveXform);
			}

			super.paintSelected(g2);

			g2.setStroke(defaultStroke);
		}
	}
}
